<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Validator;
use Intervention\Image\Facades\Image;
use Symfony\Component\HttpFoundation\Session\Session;

class UsersController extends Controller
{
    public function register()
    {
        return view('users.register');
    }

    public function store(Requests\UserRegisterRequest $request)
    {
        //保存用户数据
        $user = User::create(array_merge($request->all(),['avatar'=>'/images/default-avatar.png']));
        Auth::login($user);
        //发送验证邮件
        //重定向
        return redirect('/');
    }

    public function login()
    {
        return view('users.login');
    }

    public function logout()
    {
        \Auth::logout();
        return redirect('/');
    }

    public function signin(Requests\UserLoginRequest $request)
    {
        if(Auth::attempt([
            'email'=>$request->get('email'),
            'password'=>$request->get('password'),
//            'is_conf'=>1,
        ])|| Auth::attempt([
                'name'=>$request->get('email'),
                'password'=>$request->get('password'),
//            'is_conf'=>1,
            ])){
            if(Auth::user()->name=='admin')
                return redirect('/admin');
            else
                return redirect('/');
        }
        \Session::flash('user_login_failed','密码不正确或者邮箱没验证');
        return redirect('/user/login')->withInput();
    }

    public function avatar()
    {
        return view('users.avatar');
    }

    public function changeAvatar(Request $request)
    {
        $file = $request->file('avatar');
        $input = array('image' => $file);
        $rules = array(
            'image' => 'image'
        );
        $validator = \Validator::make($input, $rules);
        if ( $validator->fails() ) {
            return \Response::json([
                'success' => false,
                'errors' => $validator->getMessageBag()->toArray()
            ]);

        }

        $destinationPath = 'uploads/';
        $filename = \Auth::user()->id.'_'.time().$file->getClientOriginalName();

        $file->move($destinationPath,$filename);
        Image::make($destinationPath.$filename)->fit(200)->save();
//        $user = User::find(\Auth::user()->id);
//        $user->avatar = '/'.$destinationPath.$filename;
//        $user->save();
        return \Response::json([
            'sucess' => true,
            'avatar' => asset($destinationPath.$filename),
            'image' => $destinationPath.$filename,
        ]);
    }

    public function cropAvatar(Request $request)
    {
        $photo = $request->get('photo');
        $w = (int) $request->get('w');
        $h = (int) $request->get('h');
        $x = (int) $request->get('x');
        $y = (int) $request->get('y');
        Image::make($photo)->crop($w,$h,$x,$y)->save();
        $user = Auth::user();
        $user->avatar = asset($photo);
        $user->save();
        return redirect('/user/avatar');
    }

    public function qiniu()
    {
        $disk = Storage::disk('qiniu');
        $disk->put('file.jpg','11111');
        return $disk->downloadUrl('file.jpg');
    }
}
